package validator

import (
	"errors"
	"regexp"
	"strings"
)

var (
	ErrValidPhone   = errors.New("invalid phone number format")
	ErrPhoneErrLong = errors.New("email long err")

	phoneRegex = regexp.MustCompile(`^1[3-9]\d{9}$`)
)

type PhoneValiadtor struct {
	phoneLen int
}

func NewPhoneValidator() *PhoneValiadtor {
	return &PhoneValiadtor{phoneLen: 11}
}

func (p *PhoneValiadtor) Validate(phone string) error {
	phone = strings.TrimSpace(phone)

	if len(phone) != p.phoneLen {
		return ErrPhoneErrLong
	}

	if !phoneRegex.MatchString(phone) {
		return ErrValidPhone
	}

	return nil
}

func IsValidPhone(phone string) (bool, error) {
	validator := NewPhoneValidator()
	err := validator.Validate(phone)
	return err == nil, err
}
